$(function () {

    //begin列表信息
    $("#linkman_datagrid").datagrid({
        url: "/linkman/list",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "name", title: "姓名", width: 100},
            {field: "schoolName", title: "所属学校", width: 100},
            {field: "sex", title: "性别", width: 100},
            {field: "brithday", title: "生日", width: 100},
            {field: "department", title: "部门", width: 100},
            {field: "duty", title: "职务", width: 100},
            {field: "phone", title: "电话", width: 100},
            {field: "qq", title: "qq", width: 100},
            {field: "email", title: "邮箱", width: 100},
            {
                field: "status", title: "是否主联系人", width: 100,
                formatter: function (value, row, index) {
                    return status ? "是" : "否";
                }
            },
            {field: "introduction", title: "简介", width: 100},
        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber: 1,
        pageSize: 3,
        pageList: [3, 5, 10, 20],
        toolbar: "#linkman_tb"
    });
    //end列表信息

    //begin添加/编辑对话框
    $("#linkman_dialog").dialog({
        width: 500,
        height: 500,
        buttons: "#linkman_bb",
        closed: true
    });
    //end添加/编辑对话框

    //begin处理功能方法的对象
    var obj = {
        //begin添加按钮
        add: function () {
            $("#linkman_form").form("clear");
            $("#linkman_dialog").dialog("setTitle", "联系人添加");
            $("#linkman_dialog").dialog("open");
        },
        //end添加按钮

        //begin编辑按钮
        edit: function () {
            $("#linkman_form").form("clear");
            $("#linkman_dialog").dialog("setTitle", "联系人编辑");
            var row = $("#linkman_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $("#linkman_form").form("load", row);
            $("#linkman_dialog").dialog("open");
        },
        //end编辑按钮

        //begin取消按钮
        cancel: function () {
            $("#linkman_dialog").dialog("close");
        },
        //end取消按钮

        //begin保存按钮
        save: function () {
            var linkmanId = $("#linkmanId").val();
            var url;
            if (linkmanId) {
                url = "/linkman/update"
            } else {
                url = "/linkman/save"
            }
            $("#linkman_form").form("submit", {
                url: url,
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        $("#linkman_dialog").dialog("close");
                        $("#linkman_datagrid").datagrid("reload");
                    }
                }
            });
        },
        //end保存按钮

        //begin删除按钮
        remove: function () {
            var row = $("#linkman_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('确认', '您确认要操作此行吗？', function (r) {
                if (r) {
                    //注意这里的id要从当前选中的行中获取
                    var linkmanId = row.id;
                    $.get("/linkman/delete", {linkmanId: linkmanId}, function (data) {
                        if (!data.success) {
                            $.messager.alert('温馨提示', data.msg);
                        } else {
                            $.messager.alert('温馨提示', '操作成功!');
                            $("#linkman_datagrid").datagrid("reload");
                        }
                    });
                }
            });

        },
        //end删除按钮

        //begin刷新按钮
        reload: function () {
            $("#linkman_datagrid").datagrid("reload")
        },
        //end刷新按钮

        //begin查询按钮
        query: function () {
            var keywords = $("#keywords").textbox("getValue");
            var schoolName = $("#schoolName").combobox("getValue");
            $("#linkman_datagrid").datagrid("load", {
                keywords: keywords,
                schoolName: schoolName
            })
        }
        //end查询按钮
    };
    //end处理功能方法的对象

    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法

});